package com.jackstraw.jack_base.util;

import cn.hutool.core.bean.copier.CopyOptions;
import com.jackstraw.jack_base.util.exception.BadException;
import com.jackstraw.jack_base.util.exception.enums.ExceptionEnum;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class BeanUtil {

    /**
     * 将公共属性配置给目标属性
     *
     * @param publicSource 公共属性
     * @param target       目标配置
     * @return
     */
    public static Object copyPublicProperties(Object publicSource, Object target) {
        if (ObjectUtil.ObjectUtilByLang3.isEmpty(publicSource)) {
            return target;
        }

        Object publicSourceNew = null;
        try {
            publicSourceNew = publicSource.getClass().getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new BadException(BadException.bulidErrorCode(ExceptionEnum.UTIL, "001"), "copyPublicProperties异常", e);
        }

        if (ObjectUtil.ObjectUtilByLang3.isEmpty(publicSource)) {
            throw new BadException(BadException.bulidErrorCode(ExceptionEnum.UTIL, "001"), "数据拷贝异常");
        }

        BeanUtilByHutool.copyProperties(publicSource, publicSourceNew, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        BeanUtilByHutool.copyProperties(target, publicSourceNew, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        BeanUtilByHutool.copyProperties(publicSourceNew, target, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        return target;
    }

    public static class BeanUtilByHutool extends cn.hutool.core.bean.BeanUtil {
    }

    public static class BeanUtilByFasterxml extends com.fasterxml.jackson.databind.util.BeanUtil {
    }

    public static class BeanUtilBySpring extends BeanUtils {

    }

    /**
     * 实现List 集合对象的拷贝处理
     *
     * @param sources 原始对象集合
     * @param target  目标对象集合
     * @param <S>     源对象类型
     * @param <T>     目标对象类型
     * @return 拷贝后的List 集合
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
        List<T> res = new ArrayList<>(sources.size());
        for (S source : sources) {
            // 获取数据
            T obj = target.get();
            // 拷贝参数
            BeanUtilByHutool.copyProperties(source, obj);
            res.add(obj);
        }
        return res;
    }

    /**
     * 实现List 集合对象的拷贝处理
     *
     * @param sources 原始对象集合
     * @param target  目标对象集合
     * @param <S>     源对象类型
     * @param <T>     目标对象类型
     * @return 拷贝后的List 集合
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Class<T> target) {
        if (CollectionUtil.isEmpty(sources)) {
            return new ArrayList<>();
        }
        List<T> res = new ArrayList<>(sources.size());
        for (S source : sources) {
            // 获取数据
            T obj = null;
            try {
                obj = target.newInstance();
                // 拷贝参数
                BeanUtilByHutool.copyProperties(source, obj);
                res.add(obj);
            } catch (Exception e) {
                throw new BadException(BadException.bulidErrorCode(ExceptionEnum.UTIL, "001"), "copyListProperties异常", e);
            }
        }
        return res;
    }


}

